import axios from 'axios';
import { Loading, Message } from "element-ui";

const Server = axios.create({
    baseURL: process.env.VUE_APP_API_BASE_URL,
    //设置axios为form-data
    headers: {
        'Content-Type': "application/json; charset=utf-8",
        'X-Access-Token': localStorage.getItem('token') || ''
    },
    timeout: 3000,
});

// 创建loading实例
var loading = null;
// 添加请求拦截器
Server.interceptors.request.use(function (config) {
    loading = Loading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: '#fffff'
    });
    return config;
}, function (error) {
    // 对请求错误做些什么
    Message({ message: '系统提示', type: 'error' });
    console.log("========>", error);
    return Promise.reject(error);
});

// 添加响应拦截器
Server.interceptors.response.use(function (response) {
    console.log(response.data);
    // 对响应数据做点什么
    setTimeout(() => {
        loading.close();
    }, 500)
    switch (response.data.code) {
        case 500:
            Message({ message: response.data.message, type: 'error' });
            break
        default:

            break
    }
    return response;
}, function (error) {
    if (!error.response) { return }
    // 对响应错误做点什么
    switch (error.response.status) {
        case 403:
            Message({ message: '拒绝访问', type: 'error' });
            break
        case 500:
            console.log("------error.response------", error.response)
            // update-begin- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
            let type = error.response.request.responseType;
            if (type === 'blob') {
                blobToJson(data);
                break;
            }
            // update-end- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
            //notification.error({ message: '系统提示', description:'Token失效，请重新登录!',duration: 4})
            if (token && data.message.includes("Token失效")) {
                // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
                Modal.error({
                    title: '登录已过期',
                    content: '很抱歉，登录已过期，请重新登录',
                    okText: '重新登录',
                    mask: false,
                    onOk: () => {
                        store.dispatch('Logout').then(() => {
                            Vue.ls.remove(ACCESS_TOKEN)
                            try {
                                let path = window.document.location.pathname
                                console.log("location pathname -> " + path)
                                if (path != "/" && path.indexOf('/user/login') == -1) {
                                    window.location.reload()
                                }
                            } catch (e) {
                                window.location.reload()
                            }
                        })
                    }
                })
                // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
            }
            break
        case 404:
            Message({ message: '很抱歉，资源未找到!', type: 'error' });
            break
        case 504:
            Message({ message: '网络超时!', type: 'error' });
            break
        case 401:
            Message({ message: '未授权，请重新登录!', type: 'error' });
            if (token) {
                store.dispatch('Logout').then(() => {
                    setTimeout(() => {
                        window.location.reload()
                    }, 1500)
                })
            }
            break
        default:
            notification.error({
                message: '系统提示',
                description: data.message,
                duration: 4
            })
            break
    }
    loading.close();
    console.log("========>", error);
    return Promise.reject(error);
});

export default Server
